package Classes.GameViews.ViewDisplayers.MapTargets// ActionScript file
{
	import Classes.GameDatas.GD_HavenWall;
	import Classes.GameDatas.GD_Map;
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameDatas.GD_Player;
	import Classes.GameUtils.GU_GetBMPMC;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.GV_ZombieHotPanel;
	import Classes.GameViews.ViewPanels.VP_FastBuy_actPoint;
	import Classes.GameViews.ViewPanels.VP_errorPanel;
	import Classes.Interfaces.I_NeedSWFSource;
	import Classes.Managers.M_ConfigData;
	import Classes.Managers.M_LoadSWFSource;
	
	import MV_BMP.MovieClip_BMP;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	
	public class MT_HavenWall extends MapTarget implements I_NeedSWFSource
	{
		//public static const WALLGHEIGHT:Number=10;
		private var _img:DisplayObject;
		private var _hotPanel:GV_ZombieHotPanel;
		private var gdWall:GD_HavenWall;
		public function MT_HavenWall(gd:GD_HavenWall):void
		{
			super(gd);
			this.gdWall=gd;
			this.init();
			addListener();
		}
		
		private function init():void
		{
			this._hotPanel=new GV_ZombieHotPanel(this.gdWall);
			this.beginDrawMyView();
		}
		
		public static function creat(creatData:GD_HavenWall):MT_HavenWall
		{
			/*var type:int;
			if(creatObj.leftSign)
			{
			if(creatObj.upSign)
			{
			type=5;
			}
			else if(creatObj.downSign)
			{
			type=6;
			}
			else
			{
			type=1;
			}
			}
			else if(creatObj.rightSign)
			{
			if(creatObj.upSign)
			{
			type=6;
			}
			else if(creatObj.downSign)
			{
			type=7;
			}
			else
			{
			type=3;
			}
			}
			else
			{
			if(creatObj.upSign)
			{
			type=2;
			}
			else if(creatObj.downSign)
			{
			type=4;
			}
			}
			if(type)return new MT_HavenWall(type);		
			return null;
			*/
			var returnWall:MT_HavenWall=new MT_HavenWall(creatData);
			return returnWall;
		}
		
		/**
		 *覆写对象被点击方法 
		 * @param e
		 * 
		 */		
		override public function beClicked(e:MouseEvent):void
		{
			if( GV_Map.instance.buildMode	!=	GV_Map.CANNOTBUILDMODE)return;
			
			if(VP_FastBuy_actPoint.instance.check()>=3)return;
			
			//整个游戏的权限由自己控制   10000217系统权限---修围墙
			if(null==GD_Player.instance.playPowerObj[10000217] || 0==GD_Player.instance.playPowerObj[10000217])
			{
				VP_errorPanel.instance.noPowerMsg();
				return;
			}
			
			if(this.wallData.dur<(GV_Map.instance.mapData.havenLv+2))
			{
				GV_Map.instance.controlTarget.setActTarget(this);
			}
			
//			super.beClicked(null);
		}
		
		//***************
		private function sourceLoad():void
		{
			M_LoadSWFSource.instance.needLoad(this,M_ConfigData.instance.imgUrl+"zl_all.swf","lv"+this.wallData.type+"wall");
		}
		
		public function sourceLoadOver(sourceName:String):void
		{
			this.draw();
		}
		
		override protected function beginDrawMyView():void
		{
			this.sourceLoad();
		}
		
		private function draw():void
		{
			if(!this.wallData)return;
			if(this.img)this.removeChild(this.img);
			var wallsDurs:Number=0;
			var s:Number=GV_Map.instance.mapData.havenLv;
			var tmpNum:Number=(100*this.wallData.dur)/(GV_Map.instance.mapData.havenLv+2);
			if(tmpNum==0)
			{
				wallsDurs=0;
			}
			
			if(tmpNum>=100)
			{
				wallsDurs=3;
			}
			
			if(0<tmpNum && tmpNum<=50)
			{
				wallsDurs=1;
			}
			if(50<tmpNum && tmpNum<100)
			{
				wallsDurs=2;
			}
			
			var wallPicStr:String="Wall_Type"+this.wallData.type+"_Dir"+this.wallData.locationType+"_Dur"+wallsDurs
//			trace(wallPicStr);
			this._img=GU_GetBMPMC.getBMPMC(wallPicStr,false);
			this.addChild(this.img);
			this.img.y+=GV_Map.instance.gridSize/2;
			this.img.x-=GV_Map.instance.gridSize;
			
			if(!this._hotPanel)this._hotPanel=new GV_ZombieHotPanel(this.gdWall);
			this.addChild(this._hotPanel);
			
			this._hotPanel.scaleX=0.5;
			this._hotPanel.scaleY=0.5;
			this._hotPanel.y=-this._img.y-20;
			this._hotPanel.visible=false;
		}
		
		private function addListener():void
		{
			//			this.addEventListener(MouseEvent.ROLL_OVER,overHandler);
			//			this.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
		}
		
		public function removeListener():void
		{
			//			this.removeEventListener(MouseEvent.ROLL_OVER,overHandler);
			//			this.removeEventListener(MouseEvent.MOUSE_OUT,outHandler);
			this._hotPanel.release();
			this._hotPanel=null;
		}
		
		
		private function overHandler(e:MouseEvent):void
		{
			this._hotPanel.visible=true;
		}
		
		private function outHandler(e:MouseEvent):void
		{
			this._hotPanel.visible=false;
		}
		
		private function getRelevanceGridsAry(mapGrid:GD_MapGrid):Array
		{
			var relevanceGrid1:GD_MapGrid=GV_Map.instance.getCoordinateGrid(new Point(mapGrid.atx+1,mapGrid.aty));
			var relevanceGrid2:GD_MapGrid=GV_Map.instance.getCoordinateGrid(new Point(mapGrid.atx,mapGrid.aty+1));
			var relevanceGrid3:GD_MapGrid=GV_Map.instance.getCoordinateGrid(new Point(mapGrid.atx+1,mapGrid.aty+1));
			return [mapGrid,relevanceGrid1,relevanceGrid2,relevanceGrid3];
		}
		
		/**
		 *覆写碰撞检测 
		 * @param checkX
		 * @param checkY
		 * @return 
		 * 
		 */		
		override public function hitTestPointCheck(checkX:Number, checkY:Number):Boolean
		{
			if(!this.img)return false;
			if(super.hitTestPointCheck(checkX,checkY))
			{
				if(this.gdWall.dur<1)return true;
				
				var checkTarget:MovieClip_BMP=(this.img as Sprite).getChildAt(0) as MovieClip_BMP;
				return checkTarget.hitTest(new Point(checkX,checkY));
			}
			return false;
		}
		
		override public function release():void
		{
			this.removeListener();
			super.release();
			this._img=null;
			if(this._hotPanel)
			{
				this._hotPanel.release();
			}
			this._hotPanel=null;
		}
		
		/*getter & setter*/
		override public function set mapGrid(value:GD_MapGrid):void
		{
			var relevanceGridsAry:Array;
			var checkGrid:GD_MapGrid;
			if(this.mapGrid)
			{
				relevanceGridsAry=this.getRelevanceGridsAry(this.mapGrid);
				for each(checkGrid in relevanceGridsAry)
				{
					checkGrid.isWall=false;
				}
			}
			super.mapGrid=value;
			if(this.mapGrid)
			{
				relevanceGridsAry=this.getRelevanceGridsAry(this.mapGrid);
				for each(checkGrid in relevanceGridsAry)
				{
					checkGrid.isWall=true;
				}
			}
		}
		
		public function get wallData():GD_HavenWall
		{
			return this.gameData as GD_HavenWall;
		}
		
		public function get type():int
		{
			return this.wallData.type;
		}
		
		public function get img():DisplayObject
		{
			return _img;
		}
		
		override public function get selectPriority():int
		{
			return 20;
		}
		
		/**
		 * 覆定发光，加上显示热感。去掉了，mouser_out,mouser_ROLL_OVER事件 
		 * @param value
		 * 
		 */
		override public function setGlowFilter(value:Boolean):void
		{
			if(!this._hotPanel)return;
			this._hotPanel.visible	= 	value;
			super.setGlowFilter(value);
		}
		
	}
}